package flc.ast.activity;

import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.ResumeBean;
import flc.ast.databinding.ActivityEducationExpBinding;
import flc.ast.dialog.EducationBgDialog;
import flc.ast.dialog.SelDateDialog;
import flc.ast.dialog.TitleRenameDialog;
import flc.ast.utils.ResumeDataUtils;
import stark.common.basic.event.EventStatProxy;

public class EducationExpActivity extends BaseAc<ActivityEducationExpBinding> {

    public static int position;
    public static int modulePos;
    public static int dataPos;
    private boolean isAdd;
    private ResumeBean resumeBean;
    private ResumeBean.Module module;
    private ResumeBean.EducationInfoData educationInfoData;
    private ResumeBean.EducationExperience educationExperience;

    @Override
    protected int onCreate() {
        return R.layout.activity_education_exp;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivEducationExpBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);
        mDataBinding.tvTitle.setOnClickListener(this);
        mDataBinding.llStartTime.setOnClickListener(this);
        mDataBinding.llEndTime.setOnClickListener(this);
        mDataBinding.llQualification.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.tvSave) {//保存
            save();
        } else if (id == R.id.tvTitle) {//标题
            showRenameDialog();
        } else if (id == R.id.llStartTime) {//入学时间
            showStartTimeDialog();
        } else if (id == R.id.llEndTime) {//毕业时间
            showEndTimeDialog();
        } else if (id == R.id.llQualification) {//学历
            showQualificationDialog();
        }
    }

    @Override
    protected void initData() {
        resumeBean = ResumeDataUtils.getResumeList().get(position);
        module = resumeBean.getResumeData().get(modulePos);
        mDataBinding.tvTitle.setText(module.getAlias());
        if (module.getData() != null) {
            educationInfoData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.EducationInfoData.class);
            if (dataPos == -1) {
                isAdd = true;
                educationExperience = new ResumeBean.EducationExperience();
                return;
            }
            educationInfoData.setEducationExperienceList(GsonUtils.fromJson(educationInfoData.getEducationExperienceList().toString(), new TypeToken<List<ResumeBean.EducationExperience>>() {
            }.getType()));
            educationExperience = educationInfoData.getEducationExperienceList().get(dataPos);
            mDataBinding.etSchoolName.setText(educationExperience.getSchoolName());
            mDataBinding.etMajor.setText(educationExperience.getMajor());
            mDataBinding.tvStartTime.setText(educationExperience.getStartTime());
            mDataBinding.tvEndTime.setText(educationExperience.getEndTime());
            mDataBinding.tvQualification.setText(educationExperience.getQualification());
            String detail = "";
            educationExperience.setDetailList(GsonUtils.fromJson(educationExperience.getDetailList().toString(), new TypeToken<List<ResumeBean.DetailItem>>() {
            }.getType()));
            for (ResumeBean.DetailItem detailItem : educationExperience.getDetailList()) {
                detail += detailItem.getText() + "\n";
            }
            mDataBinding.etDetail.setText(detail);
        } else {
            isAdd = true;
            educationInfoData = new ResumeBean.EducationInfoData();
            educationInfoData.setEducationExperienceList(new ArrayList<>());
            educationExperience = new ResumeBean.EducationExperience();
        }
    }

    private void save() {
        String schoolName = mDataBinding.etSchoolName.getText().toString().trim();
        if (TextUtils.isEmpty(schoolName)) {
            ToastUtils.showShort(getString(R.string.please_input_school_name));
            return;
        }
        String major = mDataBinding.etMajor.getText().toString().trim();
        if (TextUtils.isEmpty(major)) {
            ToastUtils.showShort(getString(R.string.please_input_major));
            return;
        }
        String startTime = mDataBinding.tvStartTime.getText().toString().trim();
        if (TextUtils.isEmpty(startTime)) {
            ToastUtils.showShort(getString(R.string.please_sel_enrollment_time));
            return;
        }
        String endTime = mDataBinding.tvEndTime.getText().toString().trim();
        if (TextUtils.isEmpty(endTime)) {
            ToastUtils.showShort(getString(R.string.please_sel_graduation_time));
            return;
        }
        String qualification = mDataBinding.tvQualification.getText().toString().trim();
        if (TextUtils.isEmpty(qualification)) {
            ToastUtils.showShort(getString(R.string.please_sel_qualification));
            return;
        }
        String detail = mDataBinding.etDetail.getText().toString().trim();
        if (TextUtils.isEmpty(detail)) {
            ToastUtils.showShort(getString(R.string.please_input_education_exp));
            return;
        }
        String[] lines = detail.split("\\r?\\n");
        List<ResumeBean.DetailItem> list = new ArrayList<>();
        for (String line : lines) {
            list.add(new ResumeBean.DetailItem(line));
        }

        educationExperience.setSchoolName(schoolName);
        educationExperience.setMajor(major);
        educationExperience.setStartTime(startTime);
        educationExperience.setEndTime(endTime);
        educationExperience.setQualification(qualification);
        educationExperience.setDetailList(list);

        if (isAdd) {
            educationInfoData.getEducationExperienceList().add(educationExperience);
        }

        List<ResumeBean> listSave = ResumeDataUtils.getResumeList();
        module.setData(educationInfoData);
//        resumeBean.setEducationExpBean(educationExpBean);
        listSave.set(position, resumeBean);
        ResumeDataUtils.saveResumeList(listSave);
        ToastUtils.showShort(getString(R.string.save_success));
        finish();
    }

    private void showRenameDialog() {
        TitleRenameDialog renameDialog = new TitleRenameDialog(mContext);
        renameDialog.text = mDataBinding.tvTitle.getText().toString().trim();
        renameDialog.setListener(new TitleRenameDialog.Ilistener() {

            @Override
            public void clickRename(String name) {
                module.setAlias(name);
                mDataBinding.tvTitle.setText(name);
            }
        });
        renameDialog.show();
    }

    private void showStartTimeDialog() {
        SelDateDialog dialog = new SelDateDialog(mContext);
        dialog.setListener(new SelDateDialog.Ilistener() {
            @Override
            public void clickConfirm(String date) {
                mDataBinding.tvStartTime.setText(date);
            }
        });
        dialog.show();
    }

    private void showEndTimeDialog() {
        SelDateDialog dialog = new SelDateDialog(mContext);
        dialog.setListener(new SelDateDialog.Ilistener() {
            @Override
            public void clickConfirm(String date) {
                mDataBinding.tvEndTime.setText(date);
            }
        });
        dialog.show();
    }

    private void showQualificationDialog() {
        EducationBgDialog dialog = new EducationBgDialog(mContext);
        dialog.setListener(new EducationBgDialog.Ilistener() {

            @Override
            public void clickConfirm(String education) {
                mDataBinding.tvQualification.setText(education);
            }
        });
        dialog.show();
    }


}